package org.bookie.init.tasks;

import java.util.Date;
import java.util.GregorianCalendar;
import java.util.Properties;

import org.apache.hivemind.Registry;
import org.bookie.init.InitTask;
import org.bookie.model.Bet;
import org.bookie.model.Category;
import org.bookie.model.Event;
import org.bookie.model.Outcome;
import org.bookie.model.services.CategoryDAO;
import org.bookie.model.services.EventDAO;

/**
 * Loading events
 * 
 * @author zubairov
 * 
 */
public class EventsLoad implements InitTask {

	private boolean boxingNotFound = false;

	private boolean soccerNotFound = false;

	private boolean iceHockeyNotFound = false;

	private boolean formulaOneNotFound = false;

	public Properties check(Registry registry) throws Exception {
		Properties result = new Properties();
		CategoryDAO catDao = (CategoryDAO) registry
				.getService(CategoryDAO.class);
		Category category = catDao.findByName("Boxing");
		if (category == null) {
			result.put("ERROR!!!", "Please run CategoryLoad first");
		}
		if (category.getEventCount() == 0) {
			boxingNotFound = true;
			result.put("Category " + category.getName(), "EMPTY");
		} else {
			result.put("Category " + category.getName(), "OK");
		}
		category = catDao.findByName("Soccer");
		if (category == null) {
			result.put("ERROR!!!", "Please run CategoryLoad first");
		}
		if (category.getEventCount() == 0) {
			soccerNotFound = true;
			result.put("Category " + category.getName(), "EMPTY");
		} else {
			result.put("Category " + category.getName(), "OK");
		}
		category = catDao.findByName("Ice Hockey");
		if (category == null) {
			result.put("ERROR!!!", "Please run CategoryLoad first");
		}
		if (category.getEventCount() == 0) {
			iceHockeyNotFound = true;
			result.put("Category " + category.getName(), "EMPTY");
		} else {
			result.put("Category " + category.getName(), "OK");
		}
		category = catDao.findByName("Formula 1");
		if (category == null) {
			result.put("ERROR!!!", "Please run CategoryLoad first");
		}
		if (category.getEventCount() == 0) {
			formulaOneNotFound = true;
			result.put("Category " + category.getName(), "EMPTY");
		} else {
			result.put("Category " + category.getName(), "OK");
		}
		return result;
	}

	public Properties execute(Registry registry) throws Exception {
		Properties result = new Properties();
		CategoryDAO catDao = (CategoryDAO) registry
				.getService(CategoryDAO.class);
		EventDAO eventDao = (EventDAO) registry.getService(EventDAO.class);
		GregorianCalendar cal = new GregorianCalendar(2008, 1, 1, 12, 0, 0);
		if (boxingNotFound) {
			Category boxing = catDao.findByName("Boxing");
			Event event = new Event(
					"Wladimir Klitschko (UKR) - Calvin Brock (USA) (IBF/IBO Titelkampf)",
					cal.getTime(), boxing);
			Outcome klitschko = new Outcome("Wladimir Klitschko (UKR)",
					new Double(1.05));
			Outcome draw = new Outcome("Draw", new Double(21.00));
			Outcome brock = new Outcome("Calvin Brock (USA)", new Double(5.3));
			addEvent(result, eventDao, event, klitschko, draw, brock);
			Event event2 = new Event(
					"Manny Pacquiao (PHI) - Eric Morales (MEX)", cal.getTime(),
					boxing);
			Outcome pacquiao = new Outcome("Manny Pacquiao (PHI)", new Double(
					1.3));
			Outcome draw2 = new Outcome("Draw", new Double(15.00));
			Outcome morales = new Outcome("Eric Morales (MEX)", new Double(1.9));
			addEvent(result, eventDao, event2, pacquiao, draw2, morales);
		}
		if (soccerNotFound) {
			Category soccer = catDao.findByName("Soccer");
			Event event3 = new Event("FC Barcelona - Real Madrid", cal
					.getTime(), soccer);
			Outcome barca = new Outcome("FC Barcelona", new Double(1.2));
			Outcome draw3 = new Outcome("Draw", new Double(19.00));
			Outcome real = new Outcome("Real Madrid", new Double(1.5));
			addEvent(result, eventDao, event3, barca, draw3, real);

			Event event4 = new Event("Brasil - France", cal.getTime(), soccer);
			Outcome brasil = new Outcome("Brasil", new Double(1.5));
			Outcome draw4 = new Outcome("Draw", new Double(10.0));
			Outcome france = new Outcome("France", new Double(1.7));
			addEvent(result, eventDao, event4, brasil, draw4, france);
		}
		if (iceHockeyNotFound) {
			Category iceHockey = catDao.findByName("Ice Hockey");
			Event event5 = new Event("Russia - Canada", cal.getTime(),
					iceHockey);
			Outcome russia = new Outcome("Russia", new Double(1.1));
			Outcome draw5 = new Outcome("Draw", new Double(10.2));
			Outcome canada = new Outcome("Canada", new Double(2.3));
			addEvent(result, eventDao, event5, russia, draw5, canada);

			Event event6 = new Event("USA - Finland", cal.getTime(), iceHockey);
			Outcome usa = new Outcome("USA", new Double(2.0));
			Outcome draw6 = new Outcome("Draw", new Double(5.7));
			Outcome finnland = new Outcome("Finnland", new Double(1.05));
			addEvent(result, eventDao, event6, usa, draw6, finnland);
		}
		if (formulaOneNotFound) {
			Date date = new GregorianCalendar(1970, 1, 1, 12, 0, 0).getTime();
			Category formula1 = catDao.findByName("Formula 1");
			Event event6 = new Event("Winner of Monaco grand prix", date,
					formula1);
			Outcome alonzo = new Outcome("Fernando Alonzo", new Double(2.0));
			Outcome fisichella = new Outcome("Giancarlo Fisichella",
					new Double(3.0));
			Outcome kimi = new Outcome("Kimi Raikkonen", new Double(1.5));
			addEvent(result, eventDao, event6, alonzo, fisichella, kimi);

			Event event7 = new Event("Winner of Silverstone grand prix", date,
					formula1);
			addEvent(result, eventDao, event7, alonzo, fisichella, kimi);
			event7.getBets().get(0).setSuccessfulOutcome(kimi);
		}
		return result;
	}

	private void addEvent(Properties result, EventDAO eventDao, Event event,
			Outcome one, Outcome two, Outcome three) {
		Bet bet = new Bet(event, "Who wins?");
		bet.addOutcome(one, two, three);
		event.addBet(bet);
		event.setState(Event.State.ACTIVE);
		eventDao.makePersistent(event);
		result.put("Added " + event.getLabel(), "OK");
	}

	public String getTitle() {
		return "Loading events to categories";
	}

}
